﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Xml;
using Commons = NougakudoCompanion.Commons;

namespace NougakudoCompanion.Commons
{

    public partial class Runtime
    {
        Commons.Environment environment;
        Dictionary<string, Commons.Application> applications;

        Func<bool> GetAzureEnvironment;
        internal Func<bool, bool> LoadConfiguration;

        Runtime()
        {  }

        public Commons.Environment Environment { get { return environment; } set { environment = value; } }
        public Dictionary<string, Commons.Application> Applications { get { return applications; } set { applications = value; } }


        Tuple<string, Dictionary<string, Commons.Application>> GetConfiguration(string file)
        {
            var doc = new XmlDocument();
            var data = GetXmlString(file);
            if (data == "")
            {
                //Logging.Write("Runtime:Can't read {0}.", new object[] { file }, Logging.LogType.ERROR);
                return new Tuple<string, Dictionary<string, Application>>("", null);
            }
            doc.LoadXml(data);

            var nameNode = doc.SelectSingleNode("/configuration/runtime/name");
            var version = nameNode.Attributes["version"].Value;

            var applications = new Dictionary<string, Commons.Application>();
            var apps = doc.SelectNodes("/configuration/applications/application");
            if (apps != null)
            {
                foreach (XmlNode i in apps)
                {
                    Commons.Application app = new Commons.Application();
                    nameNode = i.SelectSingleNode("name");
                    app.Name = nameNode.InnerText;
                    app.Version = nameNode.Attributes["version"].Value;
                    var process = i.SelectSingleNode("process").InnerText;
                    if (process == "") process = "2";                   // default
                    app.NumOfProcess = int.Parse(process);
                    var environment = i.SelectSingleNode("environment").InnerText;
                    if (environment == "") environment = "production";  // default
                    app.Environment = environment;

                    // Add for NougakuDo 1.1.0 above.
                    var virtualHostNode = i.SelectSingleNode("virtualhost");
                    var virtualHost = "false";
                    bool host = false;
                    if (virtualHostNode != null)
                    {
                        virtualHost = virtualHostNode.InnerText;
                    }
                    if (virtualHost == "") virtualHost = "false";
                    bool.TryParse(virtualHost, out host);
                    app.VirtualHost = host;

                    applications[app.Name] = app;
                }
            }
            return new Tuple<string, Dictionary<string, Application>>(version, applications);
        }




        string GetXmlString(string fileName)
        {
            if (!File.Exists(fileName)) return "";
            try
            {
                var data = File.ReadAllText(fileName);
                if (data == "")
                    FileDelete(fileName);
                return data;
            }
            catch
            {
                //Logging.Write("Read Configuration Error :" + e.Message);
                return "";
            }
        }

        void FileDelete(string fileName)
        {
            try
            {
                if (File.Exists(fileName))
                    File.Delete(fileName);
            }
            catch { }
        }


    }
}
